package com.autolinq.service;

import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import com.autolinq.pojo.WeChatMessage;

public class WechatClickEventService extends AbstractWechatService{

	@Override
	public String handleWechatMessage(WeChatMessage message,
			String qrcodePath) {
		String returnStr = "";
		Properties prop = new Properties();// 属性集合对象
	    String classpath = this.getClass().getResource("/").getPath();
		try {
			FileInputStream fis = new FileInputStream(classpath
					+ "/prop.properties");// 属性文件输入流
			prop.load(fis);// 将属性文件流装载到Properties对象中
			fis.close();// 关闭流
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (message.getEventKey().equals("trip")) {// 旅程按钮	
			returnStr = getBackXMLTypeTripList(message,prop);	
		}else if (message.getEventKey().equals("pickup")) {// 搭车按钮	
			returnStr = getBackXMLTypePickUP(message,prop);	
		}else if (message.getEventKey().equals("mine")) {// 我的按钮	
			returnStr = getBackXMLTypeMine(message,prop);	
		}
		return returnStr;
	}
	
	private String getBackXMLTypePickUP(WeChatMessage message,Properties prop) {
		String serverURL = prop.getProperty("serverURL");
		String returnStr = "";

		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

		String times = format.format(new Date());

		Element rootXML = new Element("xml");

		rootXML.addContent(new Element("ToUserName").setText(message.getFromUserName()));

		rootXML.addContent(new Element("FromUserName").setText(message.getToUserName()));

		rootXML.addContent(new Element("CreateTime").setText(times));

		rootXML.addContent(new Element("MsgType").setText("news"));

		rootXML.addContent(new Element("ArticleCount").setText("3"));

		Element fXML = new Element("Articles");

		Element mXML = new Element("item");
		mXML.addContent(new Element("Title").setText("结伴出游1.0"));
		mXML.addContent(new Element("Description").setText(""));
		mXML.addContent(new Element("PicUrl").setText(serverURL+"/img/big_pic.png"));
		fXML.addContent(mXML);
		
		Element mXML2 = new Element("item");
		mXML2.addContent(new Element("Title").setText("我的搭车"));
		mXML2.addContent(new Element("Description").setText(""));
		mXML2.addContent(new Element("PicUrl").setText(serverURL+"/img/car.png"));
		mXML2.addContent(new Element("Url").setText(serverURL+"/car_list.html?openid="+(message.getOpenID()==null?message.getFromUserName():"")));
		fXML.addContent(mXML2);
		
		Element mXML3 = new Element("item");
		mXML3.addContent(new Element("Title").setText("添加搭车"));
		mXML3.addContent(new Element("Description").setText(""));
		mXML3.addContent(new Element("PicUrl").setText(serverURL+"/img/add_ico.png"));
		mXML3.addContent(new Element("Url").setText(serverURL+"/add_car.html?openid="+(message.getOpenID()==null?message.getFromUserName():"")));
		fXML.addContent(mXML3);

		rootXML.addContent(fXML);

		Document doc = new Document(rootXML);

		XMLOutputter XMLOut = new XMLOutputter();

		returnStr = XMLOut.outputString(doc);

		return returnStr;

	}
	
	private String getBackXMLTypeMine(WeChatMessage message,Properties prop) {
		String serverURL = prop.getProperty("serverURL");
		String returnStr = "";

		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

		String times = format.format(new Date());

		Element rootXML = new Element("xml");

		rootXML.addContent(new Element("ToUserName").setText(message.getFromUserName()));

		rootXML.addContent(new Element("FromUserName").setText(message.getToUserName()));

		rootXML.addContent(new Element("CreateTime").setText(times));

		rootXML.addContent(new Element("MsgType").setText("news"));

		rootXML.addContent(new Element("ArticleCount").setText("5"));

		Element fXML = new Element("Articles");

		Element mXML = new Element("item");
		mXML.addContent(new Element("Title").setText("结伴出游1.0"));
		mXML.addContent(new Element("Description").setText(""));
		mXML.addContent(new Element("PicUrl").setText(serverURL+"/img/big_pic.png"));
		mXML.addContent(new Element("Url").setText(serverURL+"/trip_list.html?openid="+(message.getOpenID()==null?message.getFromUserName():"")));
		fXML.addContent(mXML);
		
		
		Element mXML3 = new Element("item");
		mXML3.addContent(new Element("Title").setText("邀请好友"));
		mXML3.addContent(new Element("Description").setText(""));
		mXML3.addContent(new Element("PicUrl").setText(serverURL+"/img/invite.png"));
		mXML3.addContent(new Element("Url").setText(serverURL+"/invite_friend_go.html?openid="+(message.getOpenID()==null?message.getFromUserName():"")));
		fXML.addContent(mXML3); 
		
		
		Element mXML2 = new Element("item");
		mXML2.addContent(new Element("Title").setText("我的好友"));
		mXML2.addContent(new Element("Description").setText(""));
		mXML2.addContent(new Element("PicUrl").setText(serverURL+"/img/friend.png"));
		mXML2.addContent(new Element("Url").setText(serverURL+"/friend_list.html?openid="+(message.getOpenID()==null?message.getFromUserName():"")));
		fXML.addContent(mXML2);
		
		Element mXML4 = new Element("item");
		mXML4.addContent(new Element("Title").setText("绑定设备"));
		mXML4.addContent(new Element("Description").setText(""));
		mXML4.addContent(new Element("PicUrl").setText(serverURL+"/img/add_ico.png"));
		mXML4.addContent(new Element("Url").setText(serverURL+"/h2.html?timenow="+Calendar.getInstance()+"&openid="+(message.getOpenID()==null?message.getFromUserName():"")));
		fXML.addContent(mXML4);
		
		Element mXML5 = new Element("item");
		mXML5.addContent(new Element("Title").setText("绑定新"));
		mXML5.addContent(new Element("Description").setText(""));
		mXML5.addContent(new Element("PicUrl").setText(serverURL+"/img/add_ico.png"));
		mXML5.addContent(new Element("Url").setText(serverURL+"/scan_device.html?openid="+(message.getOpenID()==null?message.getFromUserName():"")));
		fXML.addContent(mXML5);

		rootXML.addContent(fXML);

		Document doc = new Document(rootXML);

		XMLOutputter XMLOut = new XMLOutputter();

		returnStr = XMLOut.outputString(doc);

		return returnStr;

	}
	
	public String getBackXMLTypeTripList(WeChatMessage message,Properties prop) {
		String serverURL = prop.getProperty("serverURL");
		String returnStr = "";

		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

		String times = format.format(new Date());

		Element rootXML = new Element("xml");

		rootXML.addContent(new Element("ToUserName").setText(message.getFromUserName()));

		rootXML.addContent(new Element("FromUserName").setText(message.getToUserName()));

		rootXML.addContent(new Element("CreateTime").setText(times));

		rootXML.addContent(new Element("MsgType").setText("news"));

		rootXML.addContent(new Element("ArticleCount").setText("3"));

		Element fXML = new Element("Articles");

		Element mXML = new Element("item");
		mXML.addContent(new Element("Title").setText("结伴出游1.0"));
		mXML.addContent(new Element("Description").setText(""));
		mXML.addContent(new Element("PicUrl").setText(serverURL+"/img/big_pic.png"));
		mXML.addContent(new Element("Url").setText(serverURL+"/trip_list.html?openid="+(message.getOpenID()==null?message.getFromUserName():"")));
		fXML.addContent(mXML);
		
		Element mXML2 = new Element("item");
		mXML2.addContent(new Element("Title").setText("我的旅程"));
		mXML2.addContent(new Element("Description").setText(""));
		mXML2.addContent(new Element("PicUrl").setText(serverURL+"/img/trip.png"));
		mXML2.addContent(new Element("Url").setText(serverURL+"/trip_list.html?openid="+(message.getOpenID()==null?message.getFromUserName():"")));
		fXML.addContent(mXML2);
		
		Element mXML3 = new Element("item");
		mXML3.addContent(new Element("Title").setText("添加旅程"));
		mXML3.addContent(new Element("Description").setText(""));
		mXML3.addContent(new Element("PicUrl").setText(serverURL+"/img/add_ico.png"));
		mXML3.addContent(new Element("Url").setText(serverURL+"/add_trip.html?openid="+(message.getOpenID()==null?message.getFromUserName():"")));
		fXML.addContent(mXML3);

		rootXML.addContent(fXML);

		Document doc = new Document(rootXML);

		XMLOutputter XMLOut = new XMLOutputter();

		returnStr = XMLOut.outputString(doc);

		return returnStr;

	}
	
}